/*     */ package com.zimbra.cs.mailbox.calendar;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.ParsedDateTime;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZParameter;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.Instance;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import java.text.ParseException;
/*     */ import java.util.Date;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RecurId
/*     */   implements Cloneable
/*     */ {
/*  37 */   public static int RANGE_NONE = 1;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static final int RANGE_THISANDFUTURE = 2;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static final int RANGE_THISANDPRIOR = 3;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private int mRange;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private ParsedDateTime mDateTime;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static final String FN_DT = "dt";
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static final String FN_RANGE = "r";
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static RecurId createFromInstance(CalendarItem.Instance inst)
/*     */   {
/*  92 */     return new RecurId(ParsedDateTime.fromUTCTime(inst.getStart()), RANGE_NONE);
/*     */   }
/*     */   
/*     */   public RecurId(ParsedDateTime dt, String rangeStr) {
/*  96 */     int range = RANGE_NONE;
/*  97 */     if (rangeStr != null) {
/*  98 */       if (rangeStr.equals("THISANDFUTURE")) {
/*  99 */         range = 2;
/* 100 */       } else if (rangeStr.equals("THISANDPRIOR")) {
/* 101 */         range = 3;
/*     */       }
/*     */     }
/* 104 */     this.mRange = range;
/* 105 */     this.mDateTime = dt;
/*     */   }
/*     */   
/*     */   public RecurId(ParsedDateTime dt, int range)
/*     */   {
/* 110 */     this.mRange = range;
/* 111 */     this.mDateTime = dt;
/*     */   }
/*     */   
/*     */   public Object clone() {
/* 115 */     ParsedDateTime dt = this.mDateTime != null ? (ParsedDateTime)this.mDateTime.clone() : null;
/* 116 */     return new RecurId(dt, this.mRange);
/*     */   }
/*     */   
/*     */   public String toString() {
/* 120 */     StringBuffer toRet = new StringBuffer(this.mDateTime.toString());
/* 121 */     String range = getRangeStr();
/* 122 */     if (range != null)
/* 123 */       toRet.append(";RANGE=").append(range);
/* 124 */     return toRet.toString();
/*     */   }
/*     */   
/*     */   private String getRangeStr()
/*     */   {
/* 129 */     switch (this.mRange) {
/*     */     case 2: 
/* 131 */       return "THISANDFUTURE";
/*     */     case 3: 
/* 133 */       return "THISANDPRIOR";
/*     */     }
/* 135 */     return null;
/*     */   }
/*     */   
/*     */   public boolean equals(Object other) {
/* 139 */     if (other == null) return false;
/* 140 */     RecurId rhs = (RecurId)other;
/*     */     
/* 142 */     if ((this.mRange == rhs.mRange) && (this.mDateTime.equals(rhs.mDateTime))) {
/* 143 */       return true;
/*     */     }
/*     */     
/* 146 */     return false;
/*     */   }
/*     */   
/* 149 */   public int getRange() { return this.mRange; }
/* 150 */   public ParsedDateTime getDt() { return this.mDateTime; }
/*     */   
/*     */   public String getDtZ() {
/* 153 */     return this.mDateTime.getUtcString();
/*     */   }
/*     */   
/*     */   public boolean withinRange(RecurId other) {
/* 157 */     if (other == null) {
/* 158 */       return false;
/*     */     }
/*     */     
/* 161 */     if (other.withinRange(this.mDateTime.getDate())) {
/* 162 */       return true;
/*     */     }
/*     */     
/* 165 */     return withinRange(other.mDateTime.getDate());
/*     */   }
/*     */   
/*     */   public boolean withinRange(Date d) {
/* 169 */     int comp = this.mDateTime.compareTo(d);
/* 170 */     if (comp == 0)
/* 171 */       return true;
/* 172 */     if (comp < 0) {
/* 173 */       return this.mRange == 2;
/*     */     }
/* 175 */     return this.mRange == 3;
/*     */   }
/*     */   
/*     */   public boolean withinRange(long d)
/*     */   {
/* 180 */     int comp = this.mDateTime.compareTo(d);
/* 181 */     if (comp == 0)
/* 182 */       return true;
/* 183 */     if (comp < 0) {
/* 184 */       return this.mRange == 2;
/*     */     }
/* 186 */     return this.mRange == 3;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Metadata encodeMetadata()
/*     */   {
/* 195 */     Metadata md = new Metadata();
/* 196 */     md.put("dt", this.mDateTime.toString());
/* 197 */     md.put("r", this.mRange);
/* 198 */     return md;
/*     */   }
/*     */   
/*     */ 
/*     */   public ZCalendar.ZProperty toProperty(boolean useOutlookCompatMode)
/*     */   {
/* 204 */     ZCalendar.ZProperty toRet = this.mDateTime.toProperty(ZCalendar.ICalTok.RECURRENCE_ID, useOutlookCompatMode);
/* 205 */     String range = getRangeStr();
/* 206 */     if (range != null) {
/* 207 */       toRet.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.RANGE, range));
/*     */     }
/* 209 */     return toRet;
/*     */   }
/*     */   
/*     */   public Element toXml(Element parent) {
/* 213 */     parent.addAttribute("rangeType", this.mRange);
/* 214 */     parent.addAttribute("recurId", this.mDateTime.getDateTimePartString(false));
/* 215 */     if (this.mDateTime.hasTime()) {
/* 216 */       parent.addAttribute("tz", this.mDateTime.getTZName());
/* 217 */       parent.addAttribute("ridZ", getDtZ());
/*     */     }
/* 219 */     return parent;
/*     */   }
/*     */   
/*     */   public static RecurId fromXml(Element e, TimeZoneMap tzMap) throws ServiceException {
/* 223 */     assert (tzMap != null);
/* 224 */     String recurrenceId = e.getAttribute("recurId", null);
/* 225 */     if (recurrenceId == null) return null;
/* 226 */     String tzId = e.getAttribute("tz", null);
/* 227 */     ICalTimeZone tz = tzId != null ? tzMap.lookupAndAdd(tzId) : null;
/* 228 */     String rangeType = e.getAttribute("rangeType", null);
/*     */     try {
/* 230 */       ParsedDateTime dt = ParsedDateTime.parse(recurrenceId, tzMap, tz, tzMap.getLocalTimeZone());
/* 231 */       if (rangeType != null) {
/* 232 */         return new RecurId(dt, rangeType);
/*     */       }
/* 234 */       return new RecurId(dt, RANGE_NONE);
/*     */     } catch (ParseException x) {
/* 236 */       throw ServiceException.FAILURE("recurId=" + recurrenceId + ", tz=" + tzId, x);
/*     */     }
/*     */   }
/*     */   
/*     */   public static RecurId decodeMetadata(Metadata md, TimeZoneMap tzmap) throws ServiceException {
/*     */     try {
/* 242 */       return new RecurId(ParsedDateTime.parse(md.get("dt"), tzmap), (int)md.getLong("r"));
/*     */     } catch (ParseException e) {
/* 244 */       throw ServiceException.FAILURE("Parsing " + md.get("dt"), e);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/RecurId.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */